<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@include file="/common/global.jsp"%>
<html>
<head>
	<meta charset="utf-8"/>
	<title><spring:message code="sw.sys.name"/></title>
	<meta name="description" content="<spring:message code="sw.sys.name"/>" />
	<meta name="keywords" content="<spring:message code="sw.sys.name"/>"/>
	<meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
	<link rel="stylesheet" href="${ctxPath}/static/css/amazeui.min.css" />
	<link rel="stylesheet" href="${ctxPath}/static/css/style.css" />
</head>
<body>
<header data-am-widget="header" class="am-header am-header-default header">
	<div class="am-header-left am-header-nav">
		<%--<a href="${ctxPath}/sysUser/do_member_Page" class="">
			<i class="am-header-icon am-icon-angle-left"></i>
		</a>--%>
	</div>
	<h1 class="am-header-title"> <a href="#title-link" class="" style="color: #333;">登录</a></h1>
	<div class="am-header-right am-header-nav">
		<a href="#right-link" class=""> </a>
	</div>
</header>
<div class="cate-search" style="position: relative; top: 0; border-bottom: 0;">
	<input type="text" class="cate-input" id="phoneNo" placeholder="输入手机号码" onkeyup="this.value=this.value.replace(/\D/g,'')" onblur="validatePhoneNo()">
	<input type="text" class="cate-input"id="smsCode" placeholder="输入短信验证码" onkeyup="this.value=this.value.replace(/\D/g,'')">
	<input type="button" class="cate-input" value="发送验证码" onclick="getSmsCode()">
	<input type="button" class="cate-input" value="登录" onclick="login()">
</div>

	</body>
	<%@include file="/common/footer.jsp"%>
<script>
    function validatePhoneNo() {
        var phoneNo = $("#phoneNo").val();
        if (!phoneNo) {
            var tip = "请输入手机号!";
            layer.msg(tip,{time:2000,anim:4,btn:['确定','取消']});
            return;
        }
        if(!(/^1[34578]\d{9}$/.test(phoneNo))){
            var tip = '输入的手机号格式有误';
            layer.msg(tip,{time:2000,anim:4,btn:['确定','取消']});
            return tip;
        }
    }
	
    //获取手机验证码
    function getSmsCode(){
        var phoneNo = $("#phoneNo").val();
        if(!(/^1[34578]\d{9}$/.test(phoneNo))){
            var tip = '输入的手机号有误';
            layer.msg(tip,{time:2000,anim:4,btn:['确定','取消']});
            return tip;
        }
        $.ajax({
            url:"${ctxPath}/common/smsCode",
            type:"POST",
            data:{"phoneNo":phoneNo},
			dataType:"JSON",
            success:function(data){
                logger.info(JSON.stringify(data));
                if (data.status == "200") {
                    layer.msg('发送成功！'
                        ,{
                            icon: 1,
                            time: 3000, //3s后自动关闭
                            btn: ['确定']
                        })
                } else {
                    layer.msg('发送失败！', {icon: 1});
                }
            },
            error:function(data){
                logger.error(JSON.stringify(data));
            }
        });
	}

    //用户登录
    function login(){
        if(validatePhoneNo()){return;};
        var phoneNo = $("#phoneNo").val();
        var smsCode = $("#smsCode").val();
        $.ajax({
            url:"${ctxPath}/miMember/login",
            type:"POST",
            data:{"phoneNo":phoneNo,"smsCode":smsCode},
            dataType:"JSON",
            success:function(data){
//                logger.info(JSON.stringify(data));
                if (data.status == "200") {
                    if(data.data == "lOGIN_SUCCESS"){
                        layer.msg('登录成功！'
                            ,{
                                icon: 1,
                                time: 3000, //3s后自动关闭
                                btn: ['确定']
                            }
                            , function(){
                                window.location.href = "${ctxPath}/busiCart/shoppingCartList_page";
                            });
                    }
                    if(data.data == "REGISTER_SUCCESS"){
                        layer.msg('注册成功！且登陆成功'
                            ,{
                                icon: 1,
                                time: 3000, //3s后自动关闭
                                btn: ['确定']
                            }
                            , function(){
                                window.location.href = "${ctxPath}/busiCart/shoppingCartList_page";
                            });
                    }
                } else {
                    if(data.data == "lOGIN_FAIL"){
                        layer.msg('登录失败！请重新输入验证码'
                            ,{
                                icon: 1,
                                time: 3000, //3s后自动关闭
                                btn: ['确定']
                            }
                            , function(){
                                window.location.href = "${ctxPath}/miMember/cartList_loginPage";
                            });
                    }
                    if(data.data == "REGISTER_FAIL"){
                        layer.msg('自动注册失败'
                            ,{
                                icon: 1,
                                time: 3000, //3s后自动关闭
                                btn: ['确定']
                            }
                            , function(){
                                window.location.href = "${ctxPath}/miMember/cartList_loginPage";
                            });
                    }
                }
            },
            error:function(data){
                logger.error(JSON.stringify(data));
            }
        });
    }

</script>
</html>
